| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940 |
12x
12x
12x
12x
12x
12x
12x
12x
12x
12x
1326x
1326x
12x
12x
305x
12x
1326x
1326x
12x
1326x
1326x
673x
673x
278x
395x
395x
395x
653x
15x
15x
5x
10x
10x
10x
638x
274x
274x
261x
13x
364x
364x
1326x
1326x
1326x
12x
1326x
1326x
1189x
1189x
1189x
436x
12x
921x
921x
288x
633x
633x
520x
520x
298x
298x
298x
298x
222x
222x
113x
113x
113x
113x
113x
76x
76x
76x
76x
37x
113x
109x
4x
633x
12x
559x
559x
559x
360x
199x
10x
10x
189x
189x
2x
187x
187x
187x
187x
187x
557x
557x
557x
12x
290x
290x
290x
178x
178x
112x
112x
6x
6x
106x
106x
106x
106x
84x
22x
22x
17x
17x
5x
106x
101x
101x
5x
290x
12x
24x
12x
5x
5x
12x
12x
5x
5x
12x
12x
7x
5x
12x
27x
28x
27x
27x
27x
12x
15x
15x
13x
2x
15x
15x
27x
16x
16x
16x
15x
27x
27x
11x
11x
11x
15x
12x
261x
4x
257x
257x
257x
252x
137x
115x
12x
12x
12x
103x
5x
5x
11x
11x
3x
5x
12x
364x
364x
364x
12x
13x
3x
10x
10x
10x
10x
10x
10x
8x
2x
2x
2x
10x
10x
10x
10x
12x
| /**
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Operation, OperationType } from '../operation/Operation';
import { assert, assertionError } from '@firebase/util';
import { ChildChangeAccumulator } from './ChildChangeAccumulator';
import { Change } from './Change';
import { ChildrenNode } from '../snap/ChildrenNode';
import { KEY_INDEX } from '../snap/indexes/KeyIndex';
import { ImmutableTree } from '../util/ImmutableTree';
import { Path } from '../util/Path';
import {
WriteTreeCompleteChildSource,
NO_COMPLETE_CHILD_SOURCE,
CompleteChildSource
} from './CompleteChildSource';
import { ViewCache } from './ViewCache';
import { NodeFilter } from './filter/NodeFilter';
import { WriteTreeRef } from '../WriteTree';
import { Overwrite } from '../operation/Overwrite';
import { Merge } from '../operation/Merge';
import { AckUserWrite } from '../operation/AckUserWrite';
import { Node } from '../snap/Node';
/**
* @constructor
* @struct
*/
export class ProcessorResult {
/**
* @param {!ViewCache} viewCache
* @param {!Array.<!Change>} changes
*/
constructor(
public readonly viewCache: ViewCache,
public readonly changes: Change[]
) {}
}
/**
* @constructor
*/
export class ViewProcessor {
/**
* @param {!NodeFilter} filter_
*/
constructor(private readonly filter_: NodeFilter) {}
/**
* @param {!ViewCache} viewCache
*/
assertIndexed(viewCache: ViewCache) {
assert(
viewCache
.getEventCache()
.getNode()
.isIndexed(this.filter_.getIndex()),
'Event snap not indexed'
);
assert(
viewCache
.getServerCache()
.getNode()
.isIndexed(this.filter_.getIndex()),
'Server snap not indexed'
);
}
/**
* @param {!ViewCache} oldViewCache
* @param {!Operation} operation
* @param {!WriteTreeRef} writesCache
* @param {?Node} completeCache
* @return {!ProcessorResult}
*/
applyOperation(
oldViewCache: ViewCache,
operation: Operation,
writesCache: WriteTreeRef,
completeCache: Node | null
): ProcessorResult {
const accumulator = new ChildChangeAccumulator();
let newViewCache, filterServerNode;
if (operation.type === OperationType.OVERWRITE) {
const overwrite = operation as Overwrite;
if (overwrite.source.fromUser) {
newViewCache = this.applyUserOverwrite_(
oldViewCache,
overwrite.path,
overwrite.snap,
writesCache,
completeCache,
accumulator
);
} else {
assert(overwrite.source.fromServer, 'Unknown source.');
// We filter the node if it's a tagged update or the node has been previously filtered and the
// update is not at the root in which case it is ok (and necessary) to mark the node unfiltered
// again
filterServerNode =
overwrite.source.tagged ||
(oldViewCache.getServerCache().isFiltered() &&
!overwrite.path.isEmpty());
newViewCache = this.applyServerOverwrite_(
oldViewCache,
overwrite.path,
overwrite.snap,
writesCache,
completeCache,
filterServerNode,
accumulator
);
}
} else if (operation.type === OperationType.MERGE) {
const merge = operation as Merge;
if (merge.source.fromUser) {
newViewCache = this.applyUserMerge_(
oldViewCache,
merge.path,
merge.children,
writesCache,
completeCache,
accumulator
);
} else {
assert(merge.source.fromServer, 'Unknown source.');
// We filter the node if it's a tagged update or the node has been previously filtered
filterServerNode =
merge.source.tagged || oldViewCache.getServerCache().isFiltered();
newViewCache = this.applyServerMerge_(
oldViewCache,
merge.path,
merge.children,
writesCache,
completeCache,
filterServerNode,
accumulator
);
}
} else if (operation.type === OperationType.ACK_USER_WRITE) {
const ackUserWrite = operation as AckUserWrite;
if (!ackUserWrite.revert) {
newViewCache = this.ackUserWrite_(
oldViewCache,
ackUserWrite.path,
ackUserWrite.affectedTree,
writesCache,
completeCache,
accumulator
);
} else {
newViewCache = this.revertUserWrite_(
oldViewCache,
ackUserWrite.path,
writesCache,
completeCache,
accumulator
);
}
} else Eif (operation.type === OperationType.LISTEN_COMPLETE) {
newViewCache = this.listenComplete_(
oldViewCache,
operation.path,
writesCache,
accumulator
);
} else {
throw assertionError('Unknown operation type: ' + operation.type);
}
const changes = accumulator.getChanges();
ViewProcessor.maybeAddValueEvent_(oldViewCache, newViewCache, changes);
return new ProcessorResult(newViewCache, changes);
}
/**
* @param {!ViewCache} oldViewCache
* @param {!ViewCache} newViewCache
* @param {!Array.<!Change>} accumulator
* @private
*/
private static maybeAddValueEvent_(
oldViewCache: ViewCache,
newViewCache: ViewCache,
accumulator: Change[]
) {
const eventSnap = newViewCache.getEventCache();
if (eventSnap.isFullyInitialized()) {
const isLeafOrEmpty =
eventSnap.getNode().isLeafNode() || eventSnap.getNode().isEmpty();
const oldCompleteSnap = oldViewCache.getCompleteEventSnap();
if (
accumulator.length > 0 ||
!oldViewCache.getEventCache().isFullyInitialized() ||
(isLeafOrEmpty &&
!eventSnap
.getNode()
.equals(/** @type {!Node} */ (oldCompleteSnap))) ||
!eventSnap
.getNode()
.getPriority()
.equals(oldCompleteSnap.getPriority())
) {
accumulator.push(
Change.valueChange(
/** @type {!Node} */ newViewCache.getCompleteEventSnap()
)
);
}
}
}
/**
* @param {!ViewCache} viewCache
* @param {!Path} changePath
* @param {!WriteTreeRef} writesCache
* @param {!CompleteChildSource} source
* @param {!ChildChangeAccumulator} accumulator
* @return {!ViewCache}
* @private
*/
private generateEventCacheAfterServerEvent_(
viewCache: ViewCache,
changePath: Path,
writesCache: WriteTreeRef,
source: CompleteChildSource,
accumulator: ChildChangeAccumulator
): ViewCache {
const oldEventSnap = viewCache.getEventCache();
if (writesCache.shadowingWrite(changePath) != null) {
// we have a shadowing write, ignore changes
return viewCache;
} else {
let newEventCache, serverNode;
if (changePath.isEmpty()) {
// TODO: figure out how this plays with "sliding ack windows"
assert(
viewCache.getServerCache().isFullyInitialized(),
'If change path is empty, we must have complete server data'
);
if (viewCache.getServerCache().isFiltered()) {
// We need to special case this, because we need to only apply writes to complete children, or
// we might end up raising events for incomplete children. If the server data is filtered deep
// writes cannot be guaranteed to be complete
const serverCache = viewCache.getCompleteServerSnap();
const completeChildren =
serverCache instanceof ChildrenNode
? serverCache
: ChildrenNode.EMPTY_NODE;
const completeEventChildren = writesCache.calcCompleteEventChildren(
completeChildren
);
newEventCache = this.filter_.updateFullNode(
viewCache.getEventCache().getNode(),
completeEventChildren,
accumulator
);
} else {
const completeNode = writesCache.calcCompleteEventCache(
viewCache.getCompleteServerSnap()
);
newEventCache = this.filter_.updateFullNode(
viewCache.getEventCache().getNode(),
completeNode,
accumulator
);
}
} else {
const childKey = changePath.getFront();
Iif (childKey == '.priority') {
assert(
changePath.getLength() == 1,
"Can't have a priority with additional path components"
);
const oldEventNode = oldEventSnap.getNode();
serverNode = viewCache.getServerCache().getNode();
// we might have overwrites for this priority
const updatedPriority = writesCache.calcEventCacheAfterServerOverwrite(
changePath,
oldEventNode,
serverNode
);
if (updatedPriority != null) {
newEventCache = this.filter_.updatePriority(
oldEventNode,
updatedPriority
);
} else {
// priority didn't change, keep old node
newEventCache = oldEventSnap.getNode();
}
} else {
const childChangePath = changePath.popFront();
// update child
let newEventChild;
if (oldEventSnap.isCompleteForChild(childKey)) {
serverNode = viewCache.getServerCache().getNode();
const eventChildUpdate = writesCache.calcEventCacheAfterServerOverwrite(
changePath,
oldEventSnap.getNode(),
serverNode
);
Eif (eventChildUpdate != null) {
newEventChild = oldEventSnap
.getNode()
.getImmediateChild(childKey)
.updateChild(childChangePath, eventChildUpdate);
} else {
// Nothing changed, just keep the old child
newEventChild = oldEventSnap
.getNode()
.getImmediateChild(childKey);
}
} else {
newEventChild = writesCache.calcCompleteChild(
childKey,
viewCache.getServerCache()
);
}
if (newEventChild != null) {
newEventCache = this.filter_.updateChild(
oldEventSnap.getNode(),
childKey,
newEventChild,
childChangePath,
source,
accumulator
);
} else {
// no complete child available or no change
newEventCache = oldEventSnap.getNode();
}
}
}
return viewCache.updateEventSnap(
newEventCache,
oldEventSnap.isFullyInitialized() || changePath.isEmpty(),
this.filter_.filtersNodes()
);
}
}
/**
* @param {!ViewCache} oldViewCache
* @param {!Path} changePath
* @param {!Node} changedSnap
* @param {!WriteTreeRef} writesCache
* @param {?Node} completeCache
* @param {boolean} filterServerNode
* @param {!ChildChangeAccumulator} accumulator
* @return {!ViewCache}
* @private
*/
applyServerOverwrite_(
oldViewCache: ViewCache,
changePath: Path,
changedSnap: Node,
writesCache: WriteTreeRef,
completeCache: Node | null,
filterServerNode: boolean,
accumulator: ChildChangeAccumulator
): ViewCache {
const oldServerSnap = oldViewCache.getServerCache();
let newServerCache;
const serverFilter = filterServerNode
? this.filter_
: this.filter_.getIndexedFilter();
if (changePath.isEmpty()) {
newServerCache = serverFilter.updateFullNode(
oldServerSnap.getNode(),
changedSnap,
null
);
} else if (serverFilter.filtersNodes() && !oldServerSnap.isFiltered()) {
// we want to filter the server node, but we didn't filter the server node yet, so simulate a full update
const newServerNode = oldServerSnap
.getNode()
.updateChild(changePath, changedSnap);
newServerCache = serverFilter.updateFullNode(
oldServerSnap.getNode(),
newServerNode,
null
);
} else {
const childKey = changePath.getFront();
if (
!oldServerSnap.isCompleteForPath(changePath) &&
changePath.getLength() > 1
) {
// We don't update incomplete nodes with updates intended for other listeners
return oldViewCache;
}
const childChangePath = changePath.popFront();
const childNode = oldServerSnap.getNode().getImmediateChild(childKey);
const newChildNode = childNode.updateChild(childChangePath, changedSnap);
Iif (childKey == '.priority') {
newServerCache = serverFilter.updatePriority(
oldServerSnap.getNode(),
newChildNode
);
} else {
newServerCache = serverFilter.updateChild(
oldServerSnap.getNode(),
childKey,
newChildNode,
childChangePath,
NO_COMPLETE_CHILD_SOURCE,
null
);
}
}
const newViewCache = oldViewCache.updateServerSnap(
newServerCache,
oldServerSnap.isFullyInitialized() || changePath.isEmpty(),
serverFilter.filtersNodes()
);
const source = new WriteTreeCompleteChildSource(
writesCache,
newViewCache,
completeCache
);
return this.generateEventCacheAfterServerEvent_(
newViewCache,
changePath,
writesCache,
source,
accumulator
);
}
/**
* @param {!ViewCache} oldViewCache
* @param {!Path} changePath
* @param {!Node} changedSnap
* @param {!WriteTreeRef} writesCache
* @param {?Node} completeCache
* @param {!ChildChangeAccumulator} accumulator
* @return {!ViewCache}
* @private
*/
applyUserOverwrite_(
oldViewCache: ViewCache,
changePath: Path,
changedSnap: Node,
writesCache: WriteTreeRef,
completeCache: Node | null,
accumulator: ChildChangeAccumulator
): ViewCache {
const oldEventSnap = oldViewCache.getEventCache();
let newViewCache, newEventCache;
const source = new WriteTreeCompleteChildSource(
writesCache,
oldViewCache,
completeCache
);
if (changePath.isEmpty()) {
newEventCache = this.filter_.updateFullNode(
oldViewCache.getEventCache().getNode(),
changedSnap,
accumulator
);
newViewCache = oldViewCache.updateEventSnap(
newEventCache,
true,
this.filter_.filtersNodes()
);
} else {
const childKey = changePath.getFront();
if (childKey === '.priority') {
newEventCache = this.filter_.updatePriority(
oldViewCache.getEventCache().getNode(),
changedSnap
);
newViewCache = oldViewCache.updateEventSnap(
newEventCache,
oldEventSnap.isFullyInitialized(),
oldEventSnap.isFiltered()
);
} else {
const childChangePath = changePath.popFront();
const oldChild = oldEventSnap.getNode().getImmediateChild(childKey);
let newChild;
if (childChangePath.isEmpty()) {
// Child overwrite, we can replace the child
newChild = changedSnap;
} else {
const childNode = source.getCompleteChild(childKey);
if (childNode != null) {
Iif (
childChangePath.getBack() === '.priority' &&
childNode.getChild(childChangePath.parent()).isEmpty()
) {
// This is a priority update on an empty node. If this node exists on the server, the
// server will send down the priority in the update, so ignore for now
newChild = childNode;
} else {
newChild = childNode.updateChild(childChangePath, changedSnap);
}
} else {
// There is no complete child node available
newChild = ChildrenNode.EMPTY_NODE;
}
}
if (!oldChild.equals(newChild)) {
const newEventSnap = this.filter_.updateChild(
oldEventSnap.getNode(),
childKey,
newChild,
childChangePath,
source,
accumulator
);
newViewCache = oldViewCache.updateEventSnap(
newEventSnap,
oldEventSnap.isFullyInitialized(),
this.filter_.filtersNodes()
);
} else {
newViewCache = oldViewCache;
}
}
}
return newViewCache;
}
/**
* @param {!ViewCache} viewCache
* @param {string} childKey
* @return {boolean}
* @private
*/
private static cacheHasChild_(
viewCache: ViewCache,
childKey: string
): boolean {
return viewCache.getEventCache().isCompleteForChild(childKey);
}
/**
* @param {!ViewCache} viewCache
* @param {!Path} path
* @param {ImmutableTree.<!Node>} changedChildren
* @param {!WriteTreeRef} writesCache
* @param {?Node} serverCache
* @param {!ChildChangeAccumulator} accumulator
* @return {!ViewCache}
* @private
*/
private applyUserMerge_(
viewCache: ViewCache,
path: Path,
changedChildren: ImmutableTree<Node>,
writesCache: WriteTreeRef,
serverCache: Node | null,
accumulator: ChildChangeAccumulator
): ViewCache {
// HACK: In the case of a limit query, there may be some changes that bump things out of the
// window leaving room for new items. It's important we process these changes first, so we
// iterate the changes twice, first processing any that affect items currently in view.
// TODO: I consider an item "in view" if cacheHasChild is true, which checks both the server
// and event snap. I'm not sure if this will result in edge cases when a child is in one but
// not the other.
let curViewCache = viewCache;
changedChildren.foreach((relativePath, childNode) => {
const writePath = path.child(relativePath);
if (ViewProcessor.cacheHasChild_(viewCache, writePath.getFront())) {
curViewCache = this.applyUserOverwrite_(
curViewCache,
writePath,
childNode,
writesCache,
serverCache,
accumulator
);
}
});
changedChildren.foreach((relativePath, childNode) => {
const writePath = path.child(relativePath);
if (!ViewProcessor.cacheHasChild_(viewCache, writePath.getFront())) {
curViewCache = this.applyUserOverwrite_(
curViewCache,
writePath,
childNode,
writesCache,
serverCache,
accumulator
);
}
});
return curViewCache;
}
/**
* @param {!Node} node
* @param {ImmutableTree.<!Node>} merge
* @return {!Node}
* @private
*/
private applyMerge_(node: Node, merge: ImmutableTree<Node>): Node {
merge.foreach(function(relativePath, childNode) {
node = node.updateChild(relativePath, childNode);
});
return node;
}
/**
* @param {!ViewCache} viewCache
* @param {!Path} path
* @param {!ImmutableTree.<!Node>} changedChildren
* @param {!WriteTreeRef} writesCache
* @param {?Node} serverCache
* @param {boolean} filterServerNode
* @param {!ChildChangeAccumulator} accumulator
* @return {!ViewCache}
* @private
*/
private applyServerMerge_(
viewCache: ViewCache,
path: Path,
changedChildren: ImmutableTree<Node>,
writesCache: WriteTreeRef,
serverCache: Node | null,
filterServerNode: boolean,
accumulator: ChildChangeAccumulator
): ViewCache {
// If we don't have a cache yet, this merge was intended for a previously listen in the same location. Ignore it and
// wait for the complete data update coming soon.
if (
viewCache
.getServerCache()
.getNode()
.isEmpty() &&
!viewCache.getServerCache().isFullyInitialized()
) {
return viewCache;
}
// HACK: In the case of a limit query, there may be some changes that bump things out of the
// window leaving room for new items. It's important we process these changes first, so we
// iterate the changes twice, first processing any that affect items currently in view.
// TODO: I consider an item "in view" if cacheHasChild is true, which checks both the server
// and event snap. I'm not sure if this will result in edge cases when a child is in one but
// not the other.
let curViewCache = viewCache;
let viewMergeTree;
if (path.isEmpty()) {
viewMergeTree = changedChildren;
} else {
viewMergeTree = ImmutableTree.Empty.setTree(path, changedChildren);
}
const serverNode = viewCache.getServerCache().getNode();
viewMergeTree.children.inorderTraversal((childKey, childTree) => {
if (serverNode.hasChild(childKey)) {
const serverChild = viewCache
.getServerCache()
.getNode()
.getImmediateChild(childKey);
const newChild = this.applyMerge_(serverChild, childTree);
curViewCache = this.applyServerOverwrite_(
curViewCache,
new Path(childKey),
newChild,
writesCache,
serverCache,
filterServerNode,
accumulator
);
}
});
viewMergeTree.children.inorderTraversal((childKey, childMergeTree) => {
const isUnknownDeepMerge =
!viewCache.getServerCache().isCompleteForChild(childKey) &&
childMergeTree.value == null;
if (!serverNode.hasChild(childKey) && !isUnknownDeepMerge) {
const serverChild = viewCache
.getServerCache()
.getNode()
.getImmediateChild(childKey);
const newChild = this.applyMerge_(serverChild, childMergeTree);
curViewCache = this.applyServerOverwrite_(
curViewCache,
new Path(childKey),
newChild,
writesCache,
serverCache,
filterServerNode,
accumulator
);
}
});
return curViewCache;
}
/**
* @param {!ViewCache} viewCache
* @param {!Path} ackPath
* @param {!ImmutableTree<!boolean>} affectedTree
* @param {!WriteTreeRef} writesCache
* @param {?Node} completeCache
* @param {!ChildChangeAccumulator} accumulator
* @return {!ViewCache}
* @private
*/
private ackUserWrite_(
viewCache: ViewCache,
ackPath: Path,
affectedTree: ImmutableTree<boolean>,
writesCache: WriteTreeRef,
completeCache: Node | null,
accumulator: ChildChangeAccumulator
): ViewCache {
if (writesCache.shadowingWrite(ackPath) != null) {
return viewCache;
}
// Only filter server node if it is currently filtered
const filterServerNode = viewCache.getServerCache().isFiltered();
// Essentially we'll just get our existing server cache for the affected paths and re-apply it as a server update
// now that it won't be shadowed.
const serverCache = viewCache.getServerCache();
if (affectedTree.value != null) {
// This is an overwrite.
if (
(ackPath.isEmpty() && serverCache.isFullyInitialized()) ||
serverCache.isCompleteForPath(ackPath)
) {
return this.applyServerOverwrite_(
viewCache,
ackPath,
serverCache.getNode().getChild(ackPath),
writesCache,
completeCache,
filterServerNode,
accumulator
);
} else if (ackPath.isEmpty()) {
// This is a goofy edge case where we are acking data at this location but don't have full data. We
// should just re-apply whatever we have in our cache as a merge.
let changedChildren = ImmutableTree.Empty;
serverCache.getNode().forEachChild(KEY_INDEX, function(name, node) {
changedChildren = changedChildren.set(new Path(name), node);
});
return this.applyServerMerge_(
viewCache,
ackPath,
changedChildren,
writesCache,
completeCache,
filterServerNode,
accumulator
);
} else {
return viewCache;
}
} else {
// This is a merge.
let changedChildren = ImmutableTree.Empty;
affectedTree.foreach(function(mergePath, value) {
const serverCachePath = ackPath.child(mergePath);
if (serverCache.isCompleteForPath(serverCachePath)) {
changedChildren = changedChildren.set(
mergePath,
serverCache.getNode().getChild(serverCachePath)
);
}
});
return this.applyServerMerge_(
viewCache,
ackPath,
changedChildren,
writesCache,
completeCache,
filterServerNode,
accumulator
);
}
}
/**
* @param {!ViewCache} viewCache
* @param {!Path} path
* @param {!WriteTreeRef} writesCache
* @param {!ChildChangeAccumulator} accumulator
* @return {!ViewCache}
* @private
*/
private listenComplete_(
viewCache: ViewCache,
path: Path,
writesCache: WriteTreeRef,
accumulator: ChildChangeAccumulator
): ViewCache {
const oldServerNode = viewCache.getServerCache();
const newViewCache = viewCache.updateServerSnap(
oldServerNode.getNode(),
oldServerNode.isFullyInitialized() || path.isEmpty(),
oldServerNode.isFiltered()
);
return this.generateEventCacheAfterServerEvent_(
newViewCache,
path,
writesCache,
NO_COMPLETE_CHILD_SOURCE,
accumulator
);
}
/**
* @param {!ViewCache} viewCache
* @param {!Path} path
* @param {!WriteTreeRef} writesCache
* @param {?Node} completeServerCache
* @param {!ChildChangeAccumulator} accumulator
* @return {!ViewCache}
* @private
*/
private revertUserWrite_(
viewCache: ViewCache,
path: Path,
writesCache: WriteTreeRef,
completeServerCache: Node | null,
accumulator: ChildChangeAccumulator
): ViewCache {
let complete;
if (writesCache.shadowingWrite(path) != null) {
return viewCache;
} else {
const source = new WriteTreeCompleteChildSource(
writesCache,
viewCache,
completeServerCache
);
const oldEventCache = viewCache.getEventCache().getNode();
let newEventCache;
Eif (path.isEmpty() || path.getFront() === '.priority') {
let newNode;
if (viewCache.getServerCache().isFullyInitialized()) {
newNode = writesCache.calcCompleteEventCache(
viewCache.getCompleteServerSnap()
);
} else {
const serverChildren = viewCache.getServerCache().getNode();
assert(
serverChildren instanceof ChildrenNode,
'serverChildren would be complete if leaf node'
);
newNode = writesCache.calcCompleteEventChildren(
serverChildren as ChildrenNode
);
}
newNode = newNode as Node;
newEventCache = this.filter_.updateFullNode(
oldEventCache,
newNode,
accumulator
);
} else {
const childKey = path.getFront();
let newChild = writesCache.calcCompleteChild(
childKey,
viewCache.getServerCache()
);
if (
newChild == null &&
viewCache.getServerCache().isCompleteForChild(childKey)
) {
newChild = oldEventCache.getImmediateChild(childKey);
}
if (newChild != null) {
newEventCache = this.filter_.updateChild(
oldEventCache,
childKey,
newChild,
path.popFront(),
source,
accumulator
);
} else if (
viewCache
.getEventCache()
.getNode()
.hasChild(childKey)
) {
// No complete child available, delete the existing one, if any
newEventCache = this.filter_.updateChild(
oldEventCache,
childKey,
ChildrenNode.EMPTY_NODE,
path.popFront(),
source,
accumulator
);
} else {
newEventCache = oldEventCache;
}
if (
newEventCache.isEmpty() &&
viewCache.getServerCache().isFullyInitialized()
) {
// We might have reverted all child writes. Maybe the old event was a leaf node
complete = writesCache.calcCompleteEventCache(
viewCache.getCompleteServerSnap()
);
if (complete.isLeafNode()) {
newEventCache = this.filter_.updateFullNode(
newEventCache,
complete,
accumulator
);
}
}
}
complete =
viewCache.getServerCache().isFullyInitialized() ||
writesCache.shadowingWrite(Path.Empty) != null;
return viewCache.updateEventSnap(
newEventCache,
complete,
this.filter_.filtersNodes()
);
}
}
}
|